package com.ruoyi.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.excel.ImportExcelUtil;
import com.ruoyi.system.mapper.TmcPropertyMaintenanceMapper;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TmcHouseInfoMapper;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.ITmcHouseInfoService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 房屋产权信息
 * Service业务层处理
 *
 * @author ruoyi
 * @date 2021-04-01
 */
@Service
public class TmcHouseInfoServiceImpl implements ITmcHouseInfoService {
    @Autowired
    private TmcHouseInfoMapper tmcHouseInfoMapper;

    /**
     * 查询房屋产权信息
     *
     * @param id 房屋产权信息
     *           ID
     * @return 房屋产权信息
     */
    @Override
    public TmcUserHouse selectTmcHouseInfoById(Long id) {
        return tmcHouseInfoMapper.selectTmcHouseInfoById(id);
    }

    /**
     * 查询房屋产权信息
     * 列表
     *
     * @param tmcUserHouse 房屋产权信息
     * @return 房屋产权信息
     */
    @Override
    public List<TmcUserHouse> selectTmcHouseInfoList(TmcUserHouse tmcUserHouse) {
        return tmcHouseInfoMapper.selectTmcHouseInfoList(tmcUserHouse);
    }

    @Override
    public List<Dept> queryDept() {
        return tmcHouseInfoMapper.queryDept();
    }

    /**
     * 新增房屋产权信息
     *
     * @param tmcUserHouse 房屋产权信息
     * @return 结果
     */
    @Override
    public int insertTmcHouseInfo(TmcUserHouse tmcUserHouse) {
            tmcUserHouse.setCreateTime(DateUtils.getNowDate());
            return tmcHouseInfoMapper.insertTmcHouseInfo(tmcUserHouse);
    }

    /**
     * 修改房屋产权信息
     *
     * @param tmcUserHouse 房屋产权信息
     * @return 结果
     */
    @Override
    public int updateTmcHouseInfo(TmcUserHouse tmcUserHouse) {
            tmcUserHouse.setUpdateTime(DateUtils.getNowDate());
            return tmcHouseInfoMapper.updateTmcHouseInfo(tmcUserHouse);
    }

    /**
     * 批量删除房屋产权信息
     *
     * @param ids 需要删除的房屋产权信息
     *            ID
     * @return 结果
     */
    @Override
    public int deleteTmcHouseInfoByIds(Long[] ids) {
        return tmcHouseInfoMapper.deleteTmcHouseInfoByIds(ids);
    }

    /**
     * 删除房屋产权信息
     * 信息
     *
     * @param id 房屋产权信息
     *           ID
     * @return 结果
     */
    @Override
    public int deleteTmcHouseInfoById(Long id) {
        return tmcHouseInfoMapper.deleteTmcHouseInfoById(id);
    }

    @Override
    public int selectCarnum(String ownerphone, Long companyId) {
        HashMap map = new HashMap();
        map.put("ownerphone", ownerphone);
        map.put("companyId", companyId);
        return tmcHouseInfoMapper.selectCarnum(map);
    }

    @Override
    public int importExcel(MultipartFile file) {
        String fileName = file.getName();
        List<String[]> dataList = null;
        String headData = null;
        Workbook workbook = null;
        String dateFormat = "yyyy-MM-dd";
        boolean t = true;
        int a = 0;
        try {
            //部门
            workbook = new XSSFWorkbook(file.getInputStream());
            if (workbook != null) {
                headData = ImportExcelUtil.getExcelHeadData(workbook);
                dataList = ImportExcelUtil.getExcelData(workbook, 0, 0, dateFormat);
            }
            //拿到数据进行数据库操作
            SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
            Map map = new HashMap();
            for (int j = 1; j < dataList.size(); j++) {//行
                String[] strings = dataList.get(j);
                String[] names = Convert.toStrArray("/", strings[0]);//对象基数
                //对象基数
                //产生几个对象
                if (names.length > 1) {
                    String[] tels;
                    if (!"".equals(dataList.get(j)[1]) && null != dataList.get(j)[1]) {
                         tels = Convert.toStrArray("/", strings[1]);
                    } else {
                        tels = new String[]{"", ""};
                    }
                    List<TmcUserHouse> list = new ArrayList();
                    //多条记录
                    for (int i = 0; i < names.length; i++) {
                        if (!"".equals(names[i]) && null != names[i]) {
                            TmcUserHouse house = new TmcUserHouse();
                            //名字
                            house.setOwnername(names[i]);
                            //电话
                            if (tels.length > i) {
                                house.setOwnerphone(tels[i]);
                            } else {
                                house.setOwnerphone("");
                            }
                            for (int k = 0; k < 6; k++) {//一行的每一列
                                String cellValue = dataList.get(j)[k] + "";
                                setHouse(cellValue, house, k);
                            }
                            list.add(house);
                        }
                    }
                    for (int i = 0; i < list.size(); i++) {
                        tmcHouseInfoMapper.insertTmcHouseInfo(list.get(i));
                    }
                } else {
//                    一条记录
                    TmcUserHouse house = new TmcUserHouse();
                    //名字
                    house.setOwnername(dataList.get(j)[0]);
                    //电话
                    if (!"".equals(dataList.get(j)[1]) && null != dataList.get(j)[1]) {
                        String[] strings1 = Convert.toStrArray("/", dataList.get(j)[1]);
                        house.setOwnerphone(strings1[0]);
                    } else {
                        house.setOwnerphone("");
                    }

                    house.setCompanyName(dataList.get(j)[2]);
                    house.setUnitname(dataList.get(j)[3]);
                    house.setArea(Double.valueOf(dataList.get(j)[5]));
                    house.setHousenum(dataList.get(j)[4]);
                    tmcHouseInfoMapper.insertTmcHouseInfo(house);
                }
            }
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        } finally {
        }
    }

    private void setHouse(String cellValue, TmcUserHouse entity, int i) {
        if (!"null".equals(cellValue)) {
            switch (i) {
                case 2:
                    entity.setCompanyName(cellValue);
                    break;
                case 3:
                    entity.setUnitname(cellValue);
                    break;
                case 5:
                    entity.setArea(Double.valueOf(cellValue));
                    break;
                case 4:
                    entity.setHousenum(cellValue);
                    break;
                default:
                    break;
            }

        }
    }

    @Override
    public List<TmcHouseBill> selectTmcHouseBillList(Long userID, Long houseID) {
        List<TmcHouseBill> list = tmcHouseInfoMapper.selectTmcHouseBillList(userID, houseID);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setTmcHouseSubbill(tmcHouseInfoMapper.selectTmcHouseSubbillById(list.get(i).getId()));
        }

        return list;
    }

    @Override
    public TmcHouseBill selectTmcHouseBill(Long userID, Long houseID) {
        TmcHouseBill tmcHouseBill = tmcHouseInfoMapper.selectTmcHouseBill(userID, houseID);
        if(null!=tmcHouseBill) {
            List<TmcHouseSubbill> tmcHouseSubbills = tmcHouseInfoMapper.selectTmcHouseSubbillById(tmcHouseBill.getId());
            if (tmcHouseSubbills.size() > 0) tmcHouseBill.setTmcHouseSubbill(tmcHouseSubbills);
        }
        return tmcHouseBill;
    }

    @Override
    public TmcHouseBill selectTmcHouseBillById(Long id) {
        TmcHouseBill tmcHouseBill = tmcHouseInfoMapper.selectTmcHouseBillById(id);
        tmcHouseBill.setTmcHouseSubbill(tmcHouseInfoMapper.selectTmcHouseSubbillById(tmcHouseBill.getId()));
        return tmcHouseBill;
    }

    @Override
    public int updateUserHouseBillInfo(String recordID) {
        return tmcHouseInfoMapper.updateUserHouseBillInfo(recordID);
    }

    @Override
    public int updateUserHouseBillInfoByid(Long id) {
        return tmcHouseInfoMapper.updateUserHouseBillInfoByid(id);
    }

    @Override
    public List<TmcHouseBill> queryTmcHouseBillList(TmcHouseBill tmcHouseBill) {
        return tmcHouseInfoMapper.queryTmcHouseBillList(tmcHouseBill);
    }

    @Override
    public  List queryBill(Long companyId,String year) {
        List list= new ArrayList();
        List list2= new ArrayList();
        Date date1 = new Date();
        SimpleDateFormat simpleDateFormat= new SimpleDateFormat("YYYY");
        for (int i = 1; i <13 ; i++) {
            String date;
            if(null==year) {
                if (i < 10) {
                    date = simpleDateFormat.format(date1) + "-0" + i;
                } else {
                    date = simpleDateFormat.format(date1) + "-" + i;
                }
            }else {
                if (i < 10) {
                    date = year + "-0" + i;
                } else {
                    date = year + "-" + i;
                }
            }
            HashMap HashMap = new HashMap<>();
            HashMap.put("companyId",companyId);
            HashMap.put("date",date);
            Long BillY = tmcHouseInfoMapper.querySumbyMouthY(HashMap);
            Long BillN = tmcHouseInfoMapper.querySumbyMouthN(HashMap);
            if(null==BillY)BillY=0L;
            if(null==BillN)BillN=0L;
            list.add(BillY);
            list2.add(BillN);
        }
        List list1 = new ArrayList();
        list1.add(list);
        list1.add(list2);
        return list1;
    }
    public int updateNum(String out_trade_no) {
        return tmcHouseInfoMapper.updateNum(out_trade_no);
    }


}
